package com.xiaoq;

import java.io.UnsupportedEncodingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractReceiver {
	
	private static final Logger LOG = LoggerFactory.getLogger(AbstractReceiver.class);
	
	protected abstract void execute(String message);
	
	public final void handleMessage(byte[] message) {
		String raw = null;
		try {
			raw = new String(message, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		if (LOG.isDebugEnabled()) {
			LOG.debug("message received: " + raw);
		}
		execute(raw);
	}
	
	public final void handleMessage(String message) {
		if (LOG.isDebugEnabled()) {
			LOG.debug("message received: " + message);
		}
		execute(message);
	}  
}
